* Potfolio price impact - 30 minute intervals;
* Range of X is -0.1 to +0.1 divided into 100 bins;
* Separate for Micro, Small, Large;
* and then NYSE/NASD;

libname this '\AuctionPriceImpact\Version2025';

data minor01; set this.taq_permno_cusip_linktable; keep symbol_15 date permno; 
data minor01; set minor01(rename=(symbol_15=sym_this)); 
proc sort data=minor01; by date permno;
run;

proc import datafile="TAQ.txt"
   out=minor02
   dbms=tab
   replace;
   getnames=no;
data minor02(rename=(VAR1=permno VAR2=ymd VAR3=ShrOut VAR4=LagADT VAR5=LagLnPrice VAR6=LagVolat VAR7=SZCAT VAR8=NASD VAR9=EAD)); 
	set minor02;
data minor02; 
	set minor02;
	format date yymmddn.;
	date=input(put(ymd,8.),yymmdd8.);
	drop ymd;
proc sort data=minor02; by date permno;
run;

data link;
	merge minor01 (in=a) minor02 (in=b);
  	by date permno;
  	if a and b;
run;

%macro OneMonth(dname);
	data minor01a; 
		set this.&dname;
		if timeperiod>=0 and timeperiod<390;
		timeperiod30 = ceil((timeperiod+1)/30);
		keep sym_this date timeperiod timeperiod30 Buys_v Sells_v Price;
	proc sort data=minor01a; 
		by sym_this date timeperiod30;
	run;

	proc means data=minor01a noprint; 
		by sym_this date timeperiod30;
		var Buys_v Sells_v;
		output out=minor01b(drop = _TYPE_ _FREQ_) sum(Buys_v)= Buys_v sum(Sells_v)= Sells_v;
	data minor01c; 
		set minor01a; 
		by sym_this date timeperiod30; if last.timeperiod30; 
		keep sym_this date timeperiod30 Price;
	data minor01d;
		merge minor01b(in=b) minor01c(in=c);
	  	by sym_this date timeperiod30;
		if b and c;
	run;

	data minor01e; /* create data for first price of the day */
		set minor01a; 
		by sym_this date timeperiod30; if first.timeperiod30; 
		keep sym_this date timeperiod30 Price;
	data minor01e; set minor01e(where=(timeperiod30=1));
	data minor01e; set minor01e; timeperiod30 = 0;
	run;

	data minor01f;
		set minor01d minor01e;
	proc sort data=minor01f; by sym_this date timeperiod30;
	run;

	data minor01f;
		set minor01f;
		Lsym_this = lag(sym_this);
		Ldate = lag(date);
		Ltimeperiod30 = lag(timeperiod30);
		Lprice = lag(Price);
	run;

	data minor02;
		set minor01f;
		if sym_this = Lsym_this and date = Ldate and timeperiod30 = Ltimeperiod30 + 1 then
			do;
				Ret = Price/LPrice - 1;
				q = Buys_v - Sells_v;
			end;
		else
			do;
				Ret = .;
				q = .;
			end;

		keep sym_this date timeperiod30 Ret q;
	data minor02; set minor02(where=(timeperiod30>0));
	run;

	proc sql;
		create table minor03 as 
			select *
			from minor02, link
			where minor02.sym_this=link.sym_this and minor02.date=link.date;
	quit;

	data minor04a; 
		set minor03; 
		X = (q/Shrout)/LagADT; 

		if X<-0.1 then X=.; if X>0.1 then X=.;
		if Ret<-0.1 then Ret=.; if Ret>0.1 then Ret=.; 

		drop sym_this Shrout LagADT q;
	proc sort data=minor04a; by date permno timeperiod30;
	run;

	data minor04;
		set minor04a;
		drop date timeperiod30 LagLnPrice LagVolat EAD permno;
	run;

	proc append base=final data=minor04;
	run;
%mend;

proc delete data = final; run;

libname this '\TAQIntraDay\2012';
%OneMonth(taq_month201201); %OneMonth(taq_month201202); %OneMonth(taq_month201203); %OneMonth(taq_month201204);
%OneMonth(taq_month201205); %OneMonth(taq_month201206); %OneMonth(taq_month201207); %OneMonth(taq_month201208);
%OneMonth(taq_month201209); %OneMonth(taq_month201210); %OneMonth(taq_month201211); %OneMonth(taq_month201212);

libname this '\TAQIntraDay\2013';
%OneMonth(taq_month201301); %OneMonth(taq_month201302); %OneMonth(taq_month201303); %OneMonth(taq_month201304);
%OneMonth(taq_month201305); %OneMonth(taq_month201306); %OneMonth(taq_month201307); %OneMonth(taq_month201308);
%OneMonth(taq_month201309); %OneMonth(taq_month201310); %OneMonth(taq_month201311); %OneMonth(taq_month201312);

libname this '\TAQIntraDay\2014';
%OneMonth(taq_month201401); %OneMonth(taq_month201402); %OneMonth(taq_month201403); %OneMonth(taq_month201404);
%OneMonth(taq_month201405); %OneMonth(taq_month201406); %OneMonth(taq_month201407); %OneMonth(taq_month201408);
%OneMonth(taq_month201409); %OneMonth(taq_month201410); %OneMonth(taq_month201411); %OneMonth(taq_month201412);

libname this '\TAQIntraDay\2015';
%OneMonth(taq_month201501); %OneMonth(taq_month201502); %OneMonth(taq_month201503); %OneMonth(taq_month201504);
%OneMonth(taq_month201505); %OneMonth(taq_month201506); %OneMonth(taq_month201507); %OneMonth(taq_month201508);
%OneMonth(taq_month201509); %OneMonth(taq_month201510); %OneMonth(taq_month201511); %OneMonth(taq_month201512);

libname this '\TAQIntraDay\2016';
%OneMonth(taq_month201601); %OneMonth(taq_month201602); %OneMonth(taq_month201603); %OneMonth(taq_month201604);
%OneMonth(taq_month201605); %OneMonth(taq_month201606); %OneMonth(taq_month201607); %OneMonth(taq_month201608);
%OneMonth(taq_month201609); %OneMonth(taq_month201610); %OneMonth(taq_month201611); %OneMonth(taq_month201612);

libname this '\TAQIntraDay\2017';
%OneMonth(taq_month201701); %OneMonth(taq_month201702); %OneMonth(taq_month201703); %OneMonth(taq_month201704);
%OneMonth(taq_month201705); %OneMonth(taq_month201706); %OneMonth(taq_month201707); %OneMonth(taq_month201708);
%OneMonth(taq_month201709); %OneMonth(taq_month201710); %OneMonth(taq_month201711); %OneMonth(taq_month201712);

libname this '\TAQIntraDay\2018';
%OneMonth(taq_month201801); %OneMonth(taq_month201802); %OneMonth(taq_month201803); %OneMonth(taq_month201804);
%OneMonth(taq_month201805); %OneMonth(taq_month201806); %OneMonth(taq_month201807); %OneMonth(taq_month201808);
%OneMonth(taq_month201809); %OneMonth(taq_month201810); %OneMonth(taq_month201811); %OneMonth(taq_month201812);

libname this '\TAQIntraDay\2019';
%OneMonth(taq_month201901); %OneMonth(taq_month201902); %OneMonth(taq_month201903); %OneMonth(taq_month201904);
%OneMonth(taq_month201905); %OneMonth(taq_month201906); %OneMonth(taq_month201907); %OneMonth(taq_month201908);
%OneMonth(taq_month201909); %OneMonth(taq_month201910); %OneMonth(taq_month201911); %OneMonth(taq_month201912);

libname this '\TAQIntraDay\2020';
%OneMonth(taq_month202001); %OneMonth(taq_month202002); %OneMonth(taq_month202003); %OneMonth(taq_month202004);
%OneMonth(taq_month202005); %OneMonth(taq_month202006); %OneMonth(taq_month202007); %OneMonth(taq_month202008);
%OneMonth(taq_month202009); %OneMonth(taq_month202010); %OneMonth(taq_month202011); %OneMonth(taq_month202012);

libname this '\TAQIntraDay\2021';
%OneMonth(taq_month202101); %OneMonth(taq_month202102); %OneMonth(taq_month202103); %OneMonth(taq_month202104);
%OneMonth(taq_month202105); %OneMonth(taq_month202106); %OneMonth(taq_month202107); %OneMonth(taq_month202108);
%OneMonth(taq_month202109); %OneMonth(taq_month202110); %OneMonth(taq_month202111); %OneMonth(taq_month202112);


proc sort data=final; by SZCAT;
proc rank data=final out=minor01 group=100;
	by SZCAT; 
	var X;
	ranks Xrank;
proc sort data=minor01; by SZCAT Xrank;
proc means data=minor01 noprint;
	by SZCAT Xrank; 
	var X Ret;
	output out = minor02(drop = _TYPE_ _FREQ_)
	mean = X Ret;
run;

proc sort data=final; by SZCAT NASD;
proc rank data=final out=minor03 group=100;
	by SZCAT NASD; 
	var X;
	ranks Xrank;
proc sort data=minor03; by SZCAT NASD Xrank;
proc means data=minor03 noprint;
	by SZCAT NASD Xrank; 
	var X Ret;
	output out = minor04(drop = _TYPE_ _FREQ_)
	mean = X Ret;
run;

proc export data=minor02
    outfile="Figure6a.csv"
    dbms=csv replace;
proc export data=minor04
    outfile="Figure6b.csv"
    dbms=csv replace;
run;
